package ioperations;

import book.Book;
import book.BookList;

import java.util.Scanner;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 苏李涛
 * Date: 2024-05-24
 * Time: 20:46
 */
public class AddOperation implements IOperation{
    @Override
    public void work(BookList bookList) {
        //1.判断书架（数组）是否满了
        int currentSize = bookList.getUsedSize();
        if (currentSize == bookList.getBooks().length) {
            System.out.println("该书架满了，不能放了");
            return;
        }

        //2.构建对象
        Scanner scanner = new Scanner(System.in);

        System.out.println("请输入新增的书名");
        String name = scanner.nextLine();

        System.out.println("请输入新增的作者");
        String author = scanner.nextLine();

        System.out.println("请输入新增的价格");
        int price = scanner.nextInt();

        System.out.println("请输入新增的类型");
        String type = scanner.next();
        Book newBook = new Book(name, author, price, type);


        //3.判断书架是否，已经存在这本书
        for (int i = 0; i < currentSize; i++) {
            //遍历，书架，已初始化的书
            Book book = bookList.getBook(i);
            if (book.getName().equals(name)) {
                System.out.println("书已经存在了，不用再添加了");
                return;
            }
        }

        //插入图书
        bookList.setBook(currentSize, newBook);
        bookList.setUsedSize(currentSize+1);
    }
}